<template>
  <div class="list-page-container custom-ant">
    <a-card>
      <a-form layout="inline" @keyup.enter.native="handleSearch">
        <a-row :gutter="24">
          <a-col :md="6" :sm="24">
            <CompositeSearch
              :fieldList="fieldList"
              @getParams="getSearchValue"
              @adSearch="onAdSearch"
              @adClear="onAdClear"
              @tagClick="onTagClick">
              <template v-slot:ad-search-content>
                <a-form-model
                  ref="adSearchFormRef"
                  :model="adSearchFormData"
                  layout="vertical">
                  <a-row :gutter="24">
                    <a-col :span="24">
                      <a-form-model-item label="审批状态">
                        <ASelectList
                          v-model="adSearchFormData.approveStatus"
                          :isHasDict="false"
                          :defaultOption="approveStatusOption"
                          placeholder="请选择供审批状态" />
                      </a-form-model-item>
                    </a-col>
                  </a-row>
                  <a-row :gutter="24">
                    <a-col :span="24">
                      <a-form-model-item label="创建时间">
                        <a-range-picker
                          v-model="createTime"
                          format="YYYY-MM-DD"
                          style="width:100%;"
                          @change="(timeRange, timeStringRange) => setTimeRange('createTimeStart', 'createTimeEnd', timeRange, timeStringRange)"/>
                      </a-form-model-item>
                    </a-col>
                  </a-row>
                </a-form-model>
              </template>
            </CompositeSearch>
          </a-col>
          <a-col :md="18" :sm="24">
            <div class="space-btn">
              <a-button @click="onExport">导出</a-button>
              <a-button type="primary" @click="onBatchApprove">批量审批</a-button>
            </div>
          </a-col>
        </a-row>
      </a-form>
      <a-tabs type="card" v-model="curTabValue" @change="tabChange">
        <a-tab-pane v-for="item in tabList" :key="item.key">
          <span slot="tab">
            {{ `${item.tab}` }}
            <span v-if="item.toDone">{{ `(${item.toDone})` }}</span>
            <!-- <span v-if="['2', '4'].includes(item.key) && item.toDone">{{ `（${item.toDone}）` }}</span>
            <a-badge v-if="['1', '3'].includes(item.key)" :count="item.toDone" :offset="[0, -15]" /> -->
          </span>
        </a-tab-pane>
      </a-tabs>
      <a-table
        ref="table"
        rowKey="id"
        size="middle"
        bordered
        :loading="loading"
        :columns="columns"
        :dataSource="dataSource"
        :row-selection="{ fixed: true, selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
        :scroll="{ x: '100%', autoHeight: true }"
        :pagination="pagination">
        <span slot="date" slot-scope="text">{{ text | DateFormat }}</span>
        <template slot="approveStatus" slot-scope="text">
          <span
            :class="{'cus-text': ['1'].includes(text)}"
            :warn="text === '1'">
            {{ approveStatusEnum[text] || '/' }}
          </span>
        </template>
        <template slot="action" slot-scope="text, record">
          <span v-if="['1'].includes(record.approveStatus)" class="cus-text" pointer @click="onApprove(record)">审批</span>
          <span v-if="['2', '3'].includes(record.approveStatus)" class="cus-text" pointer @click="onCheck(record)">查看</span>
        </template>
      </a-table>
    </a-card>

    <AdmittanceDetail ref="AdmittanceDetail" />
  </div>
</template>

<script>
import ListPageMixin from '@/mixins/ListPageMixin.js';
import optionsMixin from '@views/approve-manage/admittance/optionsMixin.js'
import AdmittanceDetail from '@views/supplier-manage/admittance/detail.vue'

export default {
  name: 'RegisterIndex',
  mixins: [ ListPageMixin, optionsMixin ],
  components: {
    AdmittanceDetail
  },
  data() {
    return {
      adSearchFormData: {
        approveStatus: '',
        createTimeStart: '',
        createTimeEnd: '',
      },
      createTime: [],
      curTabValue: '0',
      tabList: [
        { tab: '全部', key: '0' , toDone: 10 },
        { tab: '待审批', key: '1', toDone: 1 },
        { tab: '审批中', key: '2', toDone: 1 },
        { tab: '已审批', key: '3', toDone: 8 },
      ],
      dataSource: [],
      originalDataSource: [
        {
          approveStatus: '1',
          applyTime: '2024-06-08 11:08:30',
          approveReceiptNum: 'SPD202406001',
          approveItem: '供应商准入申请',
          applicantDepartment: '采购部',
          applicant: '杨先生',
        },
        {
          approveStatus: '2',
          applyTime: '2024-06-08 11:08:30',
          approveReceiptNum: 'SPD202406001',
          approveItem: '供应商信息变更申请',
          applicantDepartment: '采购部',
          applicant: '杨先生',
        },
        {
          approveStatus: '3',
          applyTime: '2024-06-08 11:08:30',
          approveReceiptNum: 'SPD202406001',
          approveItem: '供应商冻结申请',
          applicantDepartment: '采购部',
          applicant: '杨先生',
        },
        {
          approveStatus: '3',
          applyTime: '2024-06-08 11:08:30',
          approveReceiptNum: 'SPD202406001',
          approveItem: '供应商恢复申请',
          applicantDepartment: '采购部',
          applicant: '杨先生',
        },
        {
          approveStatus: '3',
          applyTime: '2024-06-08 11:08:30',
          approveReceiptNum: 'SPD202406001',
          approveItem: '供应商淘汰申请',
          applicantDepartment: '采购部',
          applicant: '杨先生',
        },
        {
          approveStatus: '3',
          applyTime: '2024-06-08 11:08:30',
          approveReceiptNum: 'SPD202406001',
          approveItem: '供应商黑名单申请',
          applicantDepartment: '采购部',
          applicant: '杨先生',
        },
        {
          approveStatus: '3',
          applyTime: '2024-06-08 11:08:30',
          approveReceiptNum: 'SPD202406001',
          approveItem: '供应商准入申请',
          applicantDepartment: '采购部',
          applicant: '杨先生',
        },
        {
          approveStatus: '3',
          applyTime: '2024-06-08 11:08:30',
          approveReceiptNum: 'SPD202406001',
          approveItem: '供应商准入申请',
          applicantDepartment: '采购部',
          applicant: '杨先生',
        },
        {
          approveStatus: '3',
          applyTime: '2024-06-08 11:08:30',
          approveReceiptNum: 'SPD202406001',
          approveItem: '供应商准入申请',
          applicantDepartment: '采购部',
          applicant: '杨先生',
        },
        {
          approveStatus: '3',
          applyTime: '2024-06-08 11:08:30',
          approveReceiptNum: 'SPD202406001',
          approveItem: '供应商准入申请',
          applicantDepartment: '采购部',
          applicant: '杨先生',
        }
      ]
    }
  },
  created() {
    this.handleSearch();
  },
  methods: {
    // 获取 搜索框参数
    getSearchValue(currentValue, selectFiledList) {
      console.log(currentValue, selectFiledList, 'getSearchValue');
      this.handleSearch();
    },
    // 确定搜索 高级搜索
    onAdSearch() {
      console.log(this.adSearchFormData, '确定搜索 高级搜索');
      this.handleSearch();
    },
    // 重置 高级搜索
    onAdClear() {
      console.log('重置 高级搜索');
    },
    onTagClick(data) {
      console.log(data, 'onTagClick');
      const { name } = data;
      if (['筛选组合01', '筛选组合02', '筛选组合03'].includes(name)) {
        this.adSearchFormData.approveStatus = '1';
      } else if (['筛选组合04', '筛选组合05', '筛选组合06'].includes(name)) {
        this.adSearchFormData.approveStatus = '2';
      } else {
        this.adSearchFormData.approveStatus = '3';
      }
    },
    handleSearch() {
      this.loading = true;
      setTimeout(() => {
        this.loading = false;
        this.onSelectClear();
        if (this.curTabValue === '0') {
          this.dataSource = this.originalDataSource
        } else {
          this.dataSource = this.originalDataSource.filter(item => item.approveStatus === this.curTabValue);
        }
      }, 100)
    },
    // 导出
    onExport() {

    },
    // 批量审批
    onBatchApprove() {
      console.log(this.selectedRowKeys, this.selectedRows, 'onBatchApprove');
      if (this.selectedRowKeys.length) {
        this.$confirm({
          title: '批量审批',
          content: '确认批量审批？',
          okText: '确认',
          cancelText: '取消',
          onOk: () => {
            this.handleSearch();
            this.$message.success(`操作成功`);
          }
        });
      } else {
        this.$message.error(`请选择数据！`);
      }
    },
    tabChange() {
      this.handleSearch();
    },
    // 范围日期处理
    setTimeRange(startTimeName, endTimeName, timeRange, timeStringRange) {
      if (timeStringRange.length > 0) {
        const [one, two] = timeStringRange;
        this.adSearchFormData[startTimeName] = one + ' ' + '00:00:00';
        this.adSearchFormData[endTimeName] = two + ' ' + '23:59:59';
      } else {
        this.adSearchFormData[startTimeName] = '';
        this.adSearchFormData[endTimeName] = '';
      }
    },
    onApprove(record) {
      this.$confirm({
        title: '审批',
        content: '确认审批？',
        okText: '确认',
        cancelText: '取消',
        onOk: () => {
          this.handleSearch();
          this.$message.success(`操作成功`);
        }
      });
    },
    onCheck(record) {
      this.$refs.AdmittanceDetail.open();
    }
  }
}
</script>

<style lang="less" scoped>
</style>
